package org.example.musicserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.musicserver.Request.PlayListRequest;
import org.example.musicserver.common.Result;
import org.example.musicserver.entity.PlayList;
import org.example.musicserver.mapper.PlayListMapper;
import org.example.musicserver.service.PlayListService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Service
public class PlayListServiceImpl  extends ServiceImpl<PlayListMapper, PlayList> implements PlayListService {
    @Resource
    private PlayListMapper playListMapper;
    @Override
    public List<PlayList> getAllPlayList(String query){
        QueryWrapper<PlayList> queryWrapper = new QueryWrapper<>();
        if(query != null){
            queryWrapper.like("title",query);
        }
        return playListMapper.selectList(queryWrapper);
    }
    @Override
    public Result playListOfId(Integer id){
        QueryWrapper<PlayList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return Result.success(null, playListMapper.selectOne(queryWrapper));
    };
    @Override
    public Result deletePlayList(Integer id){
        QueryWrapper<PlayList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        if (playListMapper.delete(queryWrapper) > 0) {
            return Result.success("删除歌单成功", true);
        } else {
            return Result.error("删除歌单失败");
        }
    }

    //根据列表删除多
    @Override
    public Result deletePlayLists(@RequestParam("playIds[]") List<Integer> playIds){
        int deletedCount = playListMapper.deleteBatchIds(playIds);
        if (deletedCount > 0) {
            return Result.success("删除歌单成功", true);
        } else {
            return Result.error(deletedCount+"删除歌单失败");
        }
    }
    @Override
    public Result addPlayList(PlayListRequest addSongListRequest) {
        PlayList playList = new PlayList();
        BeanUtils.copyProperties(addSongListRequest, playList);
        String pic = "/img/songListPic/123.jpg";
        playList.setPicUrl(pic);
        if (playListMapper.insert(playList) > 0) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Result updatePlayList(PlayListRequest updateRequest){
        PlayList playList = new PlayList();
        BeanUtils.copyProperties(updateRequest, playList);
        if (playListMapper.updateById(playList) > 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    @Override
    public Result selectByIds(List<Integer> ids) {

        QueryWrapper<PlayList> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids).orderByDesc("create_time");
        List<PlayList> playLists = playListMapper.selectList(queryWrapper);
        return Result.success("返回结果成功",playLists);
    }
}
